﻿package plugin.duanzao.view
{
    import cmd.*;
    import com.core.*;
    import com.sh.game.display.*;
    import com.sh.game.util.*;
    import flash.events.*;
    import manager.*;
    import morn.core.components.*;
    import morn.core.handlers.*;
    import uiresource.*;
    import view.message.*;
    import view.mornExtends.*;

    public class ZhuanYiView extends ZhuanYiUI
    {
        private var baseUI:Box;
        public var _oldArmItem:FlashItem;
        public var _newArmItem:FlashItem;
        private var index:int;
        private var armid:int;
        private var armid2:int;
        private var _cfgNewQh:Object;
        private var cfgArr:Array;
        private var _cfgitems:Object;
        private var cfgarmaddcost:Object;

        public function ZhuanYiView()
        {
            this.cfgArr = ConfigDictionary.data.armadd;
            this._cfgitems = ConfigDictionary.data.item;
            this.cfgarmaddcost = ConfigDictionary.data.armaddcost;
            return;
        }// end function

        override protected function initializeView() : void
        {
            this._oldArmItem = item1;
            this._newArmItem = item2;
            this._oldArmItem.visible = false;
            this._newArmItem.visible = false;
            slist.visible = false;
            nlist.visible = false;
            zyDescLabel.text = MColor.getHMTL("1、原装备的强化值直接覆盖到目标装备上\n", 15) + MColor.getHMTL("2、", 15) + MColor.getHMTL("原装备的转生等级直接覆盖到目标装备\n", 15) + MColor.getHMTL("3、", 15) + MColor.getHMTL("目标装备只能是未强化装备,武器之间的幸运属性可以转移 并覆盖原有祝福属性\n", 15);
            zycost.isHtml = true;
            this.initEvent();
            return;
        }// end function

        private function initEvent() : void
        {
            zhuanyiBtn.addEventListener(MouseEvent.CLICK, this.btn_sureHandler);
            this._oldArmItem.addEventListener(MouseEvent.CLICK, this.oldArmHandler);
            this._newArmItem.addEventListener(MouseEvent.CLICK, this.newArmHandler);
            return;
        }// end function

        private function newArmHandler(event:MouseEvent) : void
        {
            this._newArmItem.data = null;
            slist.visible = false;
            zycost.text = "";
            ModelProxy.change("qianghua", "changedata", {type:5});
            return;
        }// end function

        private function oldArmHandler(event:MouseEvent) : void
        {
            this._newArmItem.data = null;
            this._oldArmItem.data = null;
            slist.visible = false;
            nlist.visible = false;
            zycost.text = "";
            ModelProxy.change("qianghua", "changedata", {type:5});
            return;
        }// end function

        private function btn_sureHandler(event:MouseEvent) : void
        {
            var count:int;
            var e:* = event;
            if (this._oldArmItem.data == null || int(this._oldArmItem.data.id) == 0)
            {
                ModelProxy.change("movetips", "error", "请放入材料装备");
                return;
            }
            if (this._newArmItem.data == null || int(this._newArmItem.data.id) == 0)
            {
                ModelProxy.change("movetips", "error", "请放入目标装备");
                return;
            }
            if (int(this._oldArmItem.data.luck) > 0 && this._oldArmItem.data.cls == 1 && this._newArmItem.data.cls != 1)
            {
                Alert2.show(MColor.getHMTL("幸运属性无法转移！是否确认转移？", 16), "", ["确定", "取消"], function (param1:int, param2:Object) : void
            {
                var count:int;
                var detail:* = param1;
                var arg:* = param2;
                if (detail == 0)
                {
                    count = getCount(211131);
                    if (_oldArmItem.data.superlevel > 3)
                    {
                        if (count > 0)
                        {
                            Alert2.show(MColor.getHMTL("检查到背包中有装备转移符，是否使用装备转移符*1代替元宝进行装备转移?", 16), "", ["确定", "取消"], function (param1:int, param2:Object) : void
                {
                    if (param1 == 0)
                    {
                        zy(true);
                    }
                    else
                    {
                        zy();
                    }
                    return;
                }// end function
                );
                        }
                        else
                        {
                            zy();
                        }
                    }
                    else
                    {
                        zy();
                    }
                }
                return;
            }// end function
            );
            }
            else
            {
                count = this.getCount(211131);
                if (this._oldArmItem.data.superlevel > 3)
                {
                    if (count > 0)
                    {
                        Alert2.show(MColor.getHMTL("检查到背包中有装备转移符，是否使用装备转移符*1代替元宝进行装备转移?", 16), "", ["确定", "取消"], function (param1:int, param2:Object) : void
            {
                if (param1 == 0)
                {
                    zy(true);
                }
                else
                {
                    zy();
                }
                return;
            }// end function
            );
                    }
                    else
                    {
                        this.zy();
                    }
                }
                else
                {
                    this.zy();
                }
            }
            return;
        }// end function

        private function zy(param1:Boolean = false) : void
        {
            PluginManager.instance.sendToServer("qianghua", CMDQiangHua.Zyqh, {armid:this._oldArmItem.data.id, armid2:this._newArmItem.data.id, useitem:param1});
            this.clear();
            ModelProxy.change("qianghua", "changedata", {type:5});
            return;
        }// end function

        private function getCount(param1:int) : int
        {
            var _loc_4:Object = null;
            var _loc_5:Object = null;
            var _loc_6:int = 0;
            var _loc_2:int = 0;
            var _loc_3:* = UserData.getBagData();
            for (_loc_4 in _loc_3)
            {
                
                _loc_5 = _loc_3[_loc_4];
                if (_loc_5)
                {
                    if (_loc_5.hasOwnProperty("4"))
                    {
                        if (_loc_5[2] == 2)
                        {
                            _loc_6 = _loc_5[1];
                            _loc_5 = _loc_5[4];
                            if (_loc_5 && _loc_5.hasOwnProperty("itemid"))
                            {
                                if (_loc_5.itemid == param1)
                                {
                                    _loc_2 = _loc_2 + _loc_6;
                                }
                            }
                        }
                    }
                }
            }
            return _loc_2;
        }// end function

        public function showItem(param1:Object, param2:Boolean) : void
        {
            var _loc_3:Array = null;
            var _loc_4:Array = null;
            var _loc_5:String = null;
            if (this._oldArmItem.data == null || this._oldArmItem.data.icon == "")
            {
                _loc_3 = [];
                _loc_3 = this.getArr(param1);
                nlist.array = _loc_3;
                nlist.renderHandler = new Handler(this.nlistRender);
                nlist.visible = true;
                this._oldArmItem.data = param1;
                this._oldArmItem.visible = true;
            }
            else
            {
                if (param1.totalupvalue > 0)
                {
                    ModelProxy.change("movetips", "error", "只能转移到未强化过的装备上");
                    return;
                }
                this._newArmItem.visible = true;
                this.getRelsuperlevel(param1);
                if (this._oldArmItem.data.superlevel > 3)
                {
                    _loc_4 = (this._cfgNewQh[(this._oldArmItem.data.superlevel - 1)][0].transfercost as String).split("#");
                    _loc_5 = this._cfgitems[_loc_4[0]].name;
                    zycost.text = MColor.getHMTL("需要:", 15) + MColor.getHMTL(_loc_5 + String(_loc_4[1]), 16);
                }
                else
                {
                    zycost.text = "";
                }
            }
            ModelProxy.change("qianghua", "changedata", {type:5});
            return;
        }// end function

        private function getMaxAdd(param1:Object, param2:int = 1) : int
        {
            var _loc_3:int = 0;
            var _loc_4:int = 1;
            while (_loc_4 <= param1.superlevel)
            {
                
                if (param2 == 1)
                {
                    _loc_3 = _loc_3 + int(this.cfgArr[param1.level + "#" + _loc_4].attadd);
                }
                else if (param2 == 2)
                {
                    _loc_3 = _loc_3 + int(this.cfgArr[param1.level + "#" + _loc_4].defadd);
                }
                else if (param2 == 3)
                {
                    _loc_3 = _loc_3 + int(this.cfgArr[param1.level + "#" + _loc_4].hpadd);
                }
                else if (param2 == 4)
                {
                    _loc_3 = _loc_3 + int(this.cfgArr[param1.level + "#" + _loc_4].mpadd);
                }
                _loc_4++;
            }
            return _loc_3;
        }// end function

        private function getArr(param1:Object) : Array
        {
            var _loc_3:Object = null;
            var _loc_4:int = 0;
            var _loc_2:Array = [];
            if (param1.hp > 0)
            {
                _loc_3 = new Object();
                _loc_3.name = "血量上限:";
                _loc_4 = this.getMaxAdd(param1, 3);
                if (_loc_4 > 0)
                {
                    _loc_3.count = param1.hp + MColor.getHMTL(" +" + _loc_4, 7);
                    _loc_2.push(_loc_3);
                }
            }
            if (param1.mp > 0)
            {
                _loc_3 = new Object();
                _loc_3.name = "魔法上限:";
                _loc_4 = this.getMaxAdd(param1, 4);
                if (_loc_4 > 0)
                {
                    _loc_3.count = param1.mp + MColor.getHMTL(" +" + _loc_4, 7);
                    _loc_2.push(_loc_3);
                }
            }
            if (param1.phyAttMax > 0)
            {
                _loc_3 = new Object();
                _loc_3.name = "物理攻击:";
                _loc_4 = this.getMaxAdd(param1, 1);
                if (_loc_4 > 0)
                {
                    _loc_3.count = param1.phyAtt + "-" + param1.phyAttMax + MColor.getHMTL(" +" + _loc_4, 7);
                    _loc_2.push(_loc_3);
                }
            }
            if (param1.magicAttMax > 0)
            {
                _loc_3 = new Object();
                _loc_3.name = "魔法攻击:";
                _loc_4 = this.getMaxAdd(param1, 1);
                if (_loc_4 > 0)
                {
                    _loc_3.count = param1.magicAtt + "-" + param1.magicAttMax + MColor.getHMTL(" +" + _loc_4, 7);
                    _loc_2.push(_loc_3);
                }
            }
            if (param1.taoAttMax > 0)
            {
                _loc_3 = new Object();
                _loc_3.name = "道术攻击:";
                _loc_4 = this.getMaxAdd(param1, 1);
                if (_loc_4 > 0)
                {
                    _loc_3.count = param1.taoAtt + "-" + param1.taoAttMax + MColor.getHMTL(" +" + _loc_4, 7);
                    _loc_2.push(_loc_3);
                }
            }
            if (param1.phyDefMax > 0)
            {
                _loc_3 = new Object();
                _loc_3.name = "物理防御:";
                _loc_4 = this.getMaxAdd(param1, 2);
                if (_loc_4 > 0)
                {
                    _loc_3.count = param1.phyDef + "-" + param1.phyDefMax + MColor.getHMTL(" +" + _loc_4, 7);
                    _loc_2.push(_loc_3);
                }
            }
            if (param1.magicDefMax > 0)
            {
                _loc_3 = new Object();
                _loc_3.name = "魔法防御:";
                _loc_4 = this.getMaxAdd(param1, 2);
                if (_loc_4 > 0)
                {
                    _loc_3.count = param1.magicDef + "-" + param1.magicDefMax + MColor.getHMTL(" +" + _loc_4, 7);
                    _loc_2.push(_loc_3);
                }
            }
            return _loc_2;
        }// end function

        private function nlistRender(param1:Component, param2:int) : void
        {
            var _loc_3:Object = null;
            var _loc_4:Label = null;
            var _loc_5:Label = null;
            if (param2 < nlist.length)
            {
                _loc_3 = nlist.array[param2];
                _loc_4 = param1.getChildByName("name") as Label;
                _loc_5 = param1.getChildByName("count") as Label;
                _loc_4.text = _loc_3.name;
                _loc_5.text = _loc_3.count;
            }
            return;
        }// end function

        private function slistRender(param1:Component, param2:int) : void
        {
            var _loc_3:Object = null;
            var _loc_4:Label = null;
            var _loc_5:Label = null;
            if (param2 < slist.length)
            {
                _loc_3 = slist.array[param2];
                _loc_4 = param1.getChildByName("name") as Label;
                _loc_5 = param1.getChildByName("count") as Label;
                _loc_4.text = _loc_3.name;
                _loc_5.text = _loc_3.count;
            }
            return;
        }// end function

        private function getRelsuperlevel(param1:Object) : void
        {
            var _loc_2:* = this._oldArmItem.data;
            var _loc_3:* = new Object();
            _loc_3 = {cfg:param1, plus:new Object()};
            var _loc_4:* = _loc_2.totalupvalue;
            if (!this._cfgNewQh)
            {
                this._cfgNewQh = ConfigDictionary.data.newqh;
            }
            var _loc_5:int = 0;
            _loc_3.plus.superlevel = _loc_2.superlevel;
            _loc_3.plus.totalupvalue = _loc_4;
            _loc_3.plus.xq = _loc_2.xq;
            _loc_3.plus.luck = _loc_2.luck;
            _loc_3.plus.zs = _loc_2.zs;
            this._newArmItem.data = _loc_3.cfg;
            var _loc_6:Array = [];
            _loc_6 = this.newgetArr(_loc_3);
            slist.array = _loc_6;
            slist.renderHandler = new Handler(this.slistRender);
            slist.visible = true;
            return;
        }// end function

        private function newgetArr(param1:Object) : Array
        {
            var _loc_3:Object = null;
            var _loc_4:int = 0;
            var _loc_2:Array = [];
            if (param1.cfg.hp > 0)
            {
                _loc_3 = new Object();
                _loc_3.name = "血量上限:";
                _loc_4 = this.newgetMaxAdd(param1, 3);
                if (_loc_4 > 0)
                {
                    _loc_3.count = param1.cfg.hp + MColor.getHMTL(" +" + _loc_4, 7);
                    _loc_2.push(_loc_3);
                }
            }
            if (param1.cfg.mp > 0)
            {
                _loc_3 = new Object();
                _loc_3.name = "魔法上限:";
                _loc_4 = this.newgetMaxAdd(param1, 4);
                if (_loc_4 > 0)
                {
                    _loc_3.count = param1.cfg.mp + MColor.getHMTL(" +" + _loc_4, 7);
                    _loc_2.push(_loc_3);
                }
            }
            if (param1.cfg.phyAttMax > 0)
            {
                _loc_3 = new Object();
                _loc_3.name = "物理攻击:";
                _loc_4 = this.newgetMaxAdd(param1, 1);
                if (_loc_4 > 0)
                {
                    _loc_3.count = param1.cfg.phyAtt + "-" + param1.cfg.phyAttMax + MColor.getHMTL(" +" + _loc_4, 7);
                    _loc_2.push(_loc_3);
                }
            }
            if (param1.cfg.magicAttMax > 0)
            {
                _loc_3 = new Object();
                _loc_3.name = "魔法攻击:";
                _loc_4 = this.newgetMaxAdd(param1, 1);
                if (_loc_4 > 0)
                {
                    _loc_3.count = param1.cfg.magicAtt + "-" + param1.cfg.magicAttMax + MColor.getHMTL(" +" + _loc_4, 7);
                    _loc_2.push(_loc_3);
                }
            }
            if (param1.cfg.taoAttMax > 0)
            {
                _loc_3 = new Object();
                _loc_3.name = "道术攻击:";
                _loc_4 = this.newgetMaxAdd(param1, 1);
                if (_loc_4 > 0)
                {
                    _loc_3.count = param1.cfg.taoAtt + "-" + param1.cfg.taoAttMax + MColor.getHMTL(" +" + _loc_4, 7);
                    _loc_2.push(_loc_3);
                }
            }
            if (param1.cfg.phyDefMax > 0)
            {
                _loc_3 = new Object();
                _loc_3.name = "物理防御:";
                _loc_4 = this.newgetMaxAdd(param1, 2);
                if (_loc_4 > 0)
                {
                    _loc_3.count = param1.cfg.phyDef + "-" + param1.cfg.phyDefMax + MColor.getHMTL(" +" + _loc_4, 7);
                    _loc_2.push(_loc_3);
                }
            }
            if (param1.cfg.magicDefMax > 0)
            {
                _loc_3 = new Object();
                _loc_3.name = "魔法防御:";
                _loc_4 = this.newgetMaxAdd(param1, 2);
                if (_loc_4 > 0)
                {
                    _loc_3.count = param1.cfg.magicDef + "-" + param1.cfg.magicDefMax + MColor.getHMTL(" +" + _loc_4, 7);
                    _loc_2.push(_loc_3);
                }
            }
            return _loc_2;
        }// end function

        private function newgetMaxAdd(param1:Object, param2:int) : int
        {
            var _loc_3:int = 0;
            var _loc_4:int = 1;
            while (_loc_4 <= param1.plus.superlevel)
            {
                
                if (param2 == 1)
                {
                    _loc_3 = _loc_3 + int(this.cfgArr[param1.cfg.level + "#" + _loc_4].attadd);
                }
                else if (param2 == 2)
                {
                    _loc_3 = _loc_3 + int(this.cfgArr[param1.cfg.level + "#" + _loc_4].defadd);
                }
                else if (param2 == 3)
                {
                    _loc_3 = _loc_3 + int(this.cfgArr[param1.cfg.level + "#" + _loc_4].hpadd);
                }
                else if (param2 == 4)
                {
                    _loc_3 = _loc_3 + int(this.cfgArr[param1.cfg.level + "#" + _loc_4].mpadd);
                }
                _loc_4++;
            }
            return _loc_3;
        }// end function

        public function clear() : void
        {
            this._oldArmItem.visible = false;
            this._newArmItem.visible = false;
            this._oldArmItem.data = null;
            this._newArmItem.data = null;
            slist.visible = false;
            nlist.visible = false;
            zycost.text = "";
            return;
        }// end function

        public function zyqhback() : void
        {
            var _loc_1:* = XMLMcManager.instance.getPluginMc("qianghua", "zhuanyisuccess");
            if (_loc_1)
            {
                this.addChild(_loc_1);
                _loc_1.x = 50;
                _loc_1.y = -80;
                _loc_1.mouseChildren = false;
                _loc_1.mouseEnabled = false;
                _loc_1.playFromTo(0, (_loc_1.totalFrame - 1), new Handler(this.clipCompleteHandler, [_loc_1]));
            }
            return;
        }// end function

        protected function clipCompleteHandler(param1:XMLMovieClip) : void
        {
            if (param1)
            {
                if (param1.parent)
                {
                    param1.parent.removeChild(param1);
                }
                param1.stop();
            }
            return;
        }// end function

    }
}
